import { createRouter, createWebHashHistory } from "vue-router";
import { isAuthenticated, canAccessRoute, isAdmin } from '@/utils/auth';
import { ElMessage } from 'element-plus';

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("../views/Register.vue"),
  },
  {
    path: "/",
    redirect: "/login", // 首次进入重定向到登录页面
  },
  {
    path: "/home",
    redirect: "/home/rest",
    name: "Home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        // rest
        path: "/home/rest",
        name: "rest",
        component: () => import("../views/rest.vue"),
      },
      {
        // money
        path: "/home/money",
        name: "money",
        component: () => import("../views/money.vue"),
      },
      {
        // moneymanage
        path: "/home/moneymanage",
        name: "moneymanage",
        component: () => import("../views/moneymanage.vue"),
      },
      {
        // detail
        path: "/details/:id",
        name: "details",
        component: () => import("../views/details.vue"),
      },
      // cart
      {
        path: "/home/cart",
        name: "cart",
        component: () => import("../views/cart.vue"),
      },
      // user
      {
        path: "/home/user",
        name: "user",
        component: () => import("../views/user.vue"),
      },
      // order
      {
        path: "/home/order",
        name: "order",
        component: () => import("../views/order.vue"),
      },
      // /home/history
      {
        path: "/home/history",
        name: "history",
        component: () => import("../views/history.vue"),
      },
      // /home/help
      {
        path: "/home/help",
        name: "help",
        component: () => import("../views/help.vue"),
      },
      // manage
      {
        path: "/home/manage",
        name: "manage",
        component: () => import("../views/manage.vue"),
      },
      // dianpuxianqing
      {
        path: "/home/dianpuxiangqing/:id",
        name: "dianpuxiangqing",
        component: () => import("../views/dianpuxiangqing.vue"),
      },
      // addrest
      {
        path: "/home/addrest",
        name: "addrest",
        component: () => import("../views/addrest.vue"),
      },
      // menuList
      {
        path: "/home/menuList/:id",
        name: "menuList",
        component: () => import("../views/menuList.vue"),
      },
      // addDish
      {
        path: "/home/addDish/:id",
        name: "addDish",
        component: () => import("../views/addDish.vue"),
      },
      // dishDetail
      {
        path: "/home/dishDetail/:id",
        name: "dishDetail",
        component: () => import("../views/dishDetail.vue"),
      },

      // alluser - 管理员专用
      {
        path: "/home/alluser",
        name: "alluser",
        component: () => import("../views/alluser.vue"),
        meta: {
          requiresAuth: true,
          requiresAdmin: true,
          title: '用户管理'
        }
      },
      // allgoods - 管理员专用
      {
        path: "/home/allgoods",
        name: "allgoods",
        component: () => import("../views/allgoods.vue"),
        meta: {
          requiresAuth: true,
          requiresAdmin: true,
          title: '订单管理'
        }
      },
      // recharge-audit - 充值审核（管理员专用）
      {
        path: "/home/recharge-audit",
        name: "recharge-audit",
        component: () => import("../views/RechargeAudit.vue"),
        meta: {
          requiresAuth: true,
          requiresAdmin: true,
          title: '充值审核'
        }
      },
      // 优惠券中心
      {
        path: "/home/coupons",
        name: "coupons",
        component: () => import("../views/Coupons.vue"),
        meta: {
          requiresAuth: true,
          title: '优惠券中心'
        },
      },
      // 积分中心
      {
        path: "/home/points",
        name: "points",
        component: () => import("../views/Points.vue"),
        meta: {
          requiresAuth: true,
          title: '积分中心'
        },
      },
    ],
    meta: {
      requiresAuth: true, // 需要验证
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  // 检查用户是否已登录
  const authenticated = isAuthenticated();

  // 如果路由需要认证
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!authenticated) {
      ElMessage.warning('请先登录');
      next({ path: "/", query: { redirect: to.fullPath } });
      return;
    }

    // 检查是否需要管理员权限
    if (to.matched.some((record) => record.meta.requiresAdmin)) {
      if (!isAdmin()) {
        ElMessage.error('权限不足，需要管理员权限');
        next({ path: "/home/rest" }); // 重定向到普通用户首页
        return;
      }
    }

    // 检查路由访问权限
    if (!canAccessRoute(to.name)) {
      ElMessage.error('您没有访问该页面的权限');
      next({ path: "/home/rest" });
      return;
    }
  }

  // 如果已登录用户访问登录页，重定向到首页
  if (authenticated && (to.path === '/' || to.path === '/login')) {
    next({ path: "/home/rest" });
    return;
  }

  next();
});

export default router;
